package com.qifenqian.turbot.widget

import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.qifenqian.turbot.R
import kotlinx.android.synthetic.main.layout_direction.view.*

/**
 * @author H
 * @createTime 2018/12/19 15:13
 * @description 封装的餐厅管理中的菜单项
 * @see
 * @since
 */
class ItemDirectionLayout : LinearLayout {

    private var textLeft: String? = null
    private var textRight: String? = null

    constructor(context: Context) : super(context) {
        initAttrs(context, null, 0, 0)
    }

    private var view: View? = null
    private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) {
        view = LayoutInflater.from(context).inflate(R.layout.layout_direction, this, false)
        addView(view)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemDirectionLayout, defStyleAttr, 0)
        textLeft = typedArray.getString(R.styleable.ItemDirectionLayout_textLeft)
        textRight = typedArray.getString(R.styleable.ItemDirectionLayout_textRight)
        val textRightColor = typedArray.getColor(R.styleable.ItemDirectionLayout_textRightColor, resources.getColor(R.color.gray_666666))
        val hasLine = typedArray.getBoolean(R.styleable.ItemDirectionLayout_hasLine, false)
        view!!.tv_direction_left.text = textLeft
        view!!.tv_direction_right.text = textRight
        view!!.tv_direction_right.setTextColor(textRightColor)
        val visibility: Int = if (hasLine) View.VISIBLE else View.GONE
        view!!.line_direction.visibility = visibility
        typedArray.recycle()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initAttrs(context, attrs, 0, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initAttrs(context, attrs, defStyleAttr, 0)
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initAttrs(context, attrs, defStyleAttr, defStyleRes)
    }

    internal fun setTextLeft(textLeft: String) {
        view!!.tv_direction_left.text = textLeft
    }

    internal fun setTextRight(textRight: String) {
        view!!.tv_direction_right.text = textRight
    }

    internal fun setTextRightColor(textRightColor: Int) {
        view!!.tv_direction_right.setTextColor(textRightColor)
    }
}